#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using FreeOnlineApplication.Class.Entity;
using MySql.Data.MySqlClient;
using System.Configuration;

#endregion

namespace FreeOnlineApplication
{
    public partial class Hotel : Form, IMainForm
    {
        UserControlFactory m_userControlFactory;
        User m_user = null;
        Unit m_unit = null;
        MySqlConnection m_connection;

        public Hotel()
        {
            InitializeComponent();
            menuItemNew.Enabled = false;
            menuItemSave.Enabled = false;
            menuItemEdit.Enabled = false;
            menuItemDelete.Enabled = false;


            menuItemNew.Shortcut = Shortcut.CtrlShiftN;
            menuItemSave.Shortcut = Shortcut.CtrlShiftS;
            menuItemEdit.Shortcut = Shortcut.CtrlShiftE;
            menuItemDelete.Shortcut = Shortcut.CtrlShiftD;

            treeNodeUser.AfterSelect += new TreeViewEventHandler(treeNodeUser_AfterSelect);
            treeNodeCompany.AfterSelect += new TreeViewEventHandler(treeNodeCompany_AfterSelect);
            treeNodeRoomType.AfterSelect += new TreeViewEventHandler(treeNodeRoomType_AfterSelect);
            treeNodeRoomMaster.AfterSelect += new TreeViewEventHandler(treeNodeRoomMaster_AfterSelect);
            treeNodeFood.AfterSelect += new TreeViewEventHandler(treeNodeFood_AfterSelect);
            treeNodeLaundry.AfterSelect += new TreeViewEventHandler(treeNodeLaundry_AfterSelect);
            treeNodeCustomer.AfterSelect += new TreeViewEventHandler(treeNodeCustomer_AfterSelect);
            treeNodeBooking.AfterSelect += new TreeViewEventHandler(treeNodeBooking_AfterSelect);
            treeNodeSupplier.AfterSelect += new TreeViewEventHandler(treeNodeSupplier_AfterSelect);
            treeNodeFacility.AfterSelect += new TreeViewEventHandler(treeNodeFacility_AfterSelect);
            treeNodeCheckIn.AfterSelect += new TreeViewEventHandler(treeNodeCheckIn_AfterSelect);
            treeNodeAvailable.AfterSelect += new TreeViewEventHandler(treeNodeAvailable_AfterSelect);
            treeNodeFacilityTransaction.AfterSelect += new TreeViewEventHandler(treeNodeFacilityTransaction_AfterSelect);

            splitContainer1.Visible = false;
            menuItemLogOut.Visible = false;
        }

        void treeNodeFacilityTransaction_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.FACILITYTRANSACTION);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeAvailable_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //AvailableRoomForm fr = new AvailableRoomForm(this);
            //fr.ShowDialog();
            try
            {
                loadUserControl(UserControlFactory.CHECKAVAILABLEROOM);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeCheckIn_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CHECKIN);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeFacility_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.FACILITY);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeSupplier_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.SUPPLIER);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeBooking_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.BOOKING);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeCustomer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.CUSTOMER);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeLaundry_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.LAUNDRY);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            } 
        }

        void treeNodeFood_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.FOOD);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            }
        }

        void treeNodeRoomMaster_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.ROOM_MASTER);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            }
        }

        void treeNodeRoomType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.ROOM_TYPE);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            }
        }

        void treeNodeCompany_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.COMPANY_PROFILE);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            }
        }

        void treeNodeUser_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                loadUserControl(UserControlFactory.USER);
            }
            catch (Exception x)
            {
                statusBar1.Text = x.Message;
            }
        }
        private void loadUserControl(string p)
        {
            RefreshConnection();
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(m_userControlFactory.GetUserControl(p, this));
            IChildForm c = (IChildForm)splitContainer1.Panel2.Controls[0];
            c.LoadEnableButton();
        }
        #region IMainForm Members

        public void SetEnableMenuButton(bool save, bool news, bool delete, bool edit)
        {
            menuItemNew.Enabled = news;
            menuItemSave.Enabled = save;
            menuItemEdit.Enabled = edit;
            menuItemDelete.Enabled = delete;
        }

        public User CurrentUser
        {
            get
            {
                return m_user;
            }
            set
            {
                m_user = value;
            }
        }

        public Unit CurrentUnit
        {
            get
            {
                return m_unit;
            }
            set
            {
                m_unit = value;
            }
        }

        #endregion

        private void menuItemLogin_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Login is CLICKED!!!....");
            RefreshConnection();
            LoginForm loginForm = new LoginForm(this);
            loginForm.ShowDialog();
            //TestForm t = new TestForm();
            //t.ShowDialog();
          
        }

        private void menuItemRegister_Click(object sender, EventArgs e)
        {
            RefreshConnection();
            RegisterForm frm = new RegisterForm(this);
            frm.ShowDialog();
        }

        public void EnableSplitContainer()
        {
            splitContainer1.Visible = m_user != null;
            menuItemLogin.Visible = !splitContainer1.Visible;
            menuItemLogOut.Visible = splitContainer1.Visible;
            menuItemRegister.Visible = menuItemLogin.Visible;
            if (splitContainer1.Visible)
            {
                splitContainer1.Panel2.Controls.Clear();
                m_userControlFactory = new UserControlFactory();
                menuItemNew.Enabled = false;
                menuItemSave.Enabled = false;
                menuItemEdit.Enabled = false;
                menuItemDelete.Enabled = false;
                labeluser.Text = "Welcome " + m_user.NAME + @" [" + m_unit.NAME + @"], you logged on at " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss");
            }

        }
        private void menuItemLogOut_Click(object sender, EventArgs e)
        {
            m_user = null;
            m_unit = null;
            menuItemNew.Enabled = false;
            menuItemSave.Enabled = false;
            menuItemEdit.Enabled = false;
            menuItemDelete.Enabled = false;
            labeluser.Text = "";
            statusBar1.Text = "";
            EnableSplitContainer();
        }

        private void menuItemNew_Click(object sender, EventArgs e)
        {
            IChildForm c = (IChildForm)splitContainer1.Panel2.Controls[0];
            if (c == null) return;
            c.New();
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {
            IChildForm c = (IChildForm)splitContainer1.Panel2.Controls[0];
            if (c == null) return;
            c.Save();
        }

        private void menuItemEdit_Click(object sender, EventArgs e)
        {
            IChildForm c = (IChildForm)splitContainer1.Panel2.Controls[0];
            if (c == null) return;
            c.Edit();
        }

        private void menuItemDelete_Click(object sender, EventArgs e)
        {
            IChildForm c = (IChildForm)splitContainer1.Panel2.Controls[0];
            if (c == null) return;
            c.Delete();
        }

        #region IMainForm Members


        public MySqlConnection Connection
        {
            get {
                RefreshConnection();
                return m_connection; 
            }
        }

        public void RefreshConnection()
        {
            if (m_connection == null)
            {
                m_connection = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                m_connection.Open();
            }
            if (m_connection.State == ConnectionState.Closed)
                m_connection.Open();
            if (m_connection.State == ConnectionState.Broken)
            {
                m_connection = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
                m_connection.Open();
            }
        }

        #endregion

        private void Hotel_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_connection.Close();
        }
    }
}